package com.trjcn.wxshare.utils;

import com.trjcn.wxshare.enums.AutoRespReqTypeEnum;
import com.trjcn.wxshare.reqeust.AutoMsgReq;
import com.trjcn.wxshare.reqeust.CustomerMsgReq;

import static com.trjcn.wxshare.enums.AutoRespReqTypeEnum.MessageType;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class WXAutoRespReqUtil {

    /**
     * 根据消息类型构造返回消息
     * @param map 封装了解析结果的Map
     * @return responseMessage(响应消息)
     */
    public static CustomerMsgReq buildResponseMessage(Map map, String appId, String href, String text) {
        //响应消息
        CustomerMsgReq customerMsgReq = null;
        //得到消息类型
        String msgType = map.get("MsgType").toString();
        //消息类型
        MessageType messageEnumType = MessageType.valueOf(MessageType.class, msgType.toUpperCase());
        switch (messageEnumType) {
                //处理文本消息
            case TEXT:
                //图片
            case IMAGE:
                //一个页面
            case MINIPROGRAMPAGE:
                customerMsgReq = handleTextMessage(map, appId, href, text);
                break;
                //处理图片消息
            /* case VOICE:
                //处理语音消息
                responseMessage = handleVoiceMessage(map);
                break;
            case VIDEO:
                //处理视频消息
                responseMessage = handleVideoMessage(map);
                break;
            case SHORTVIDEO:
                //处理小视频消息
                responseMessage = handleSmallVideoMessage(map);
                break;
            case LOCATION:
                //处理位置消息
                responseMessage = handleLocationMessage(map);
                break;
            case LINK:
                //处理链接消息
                responseMessage = handleLinkMessage(map);
                break;
            case EVENT:
                //处理事件消息,用户在关注与取消关注公众号时，微信会向我们的公众号服务器发送事件消息,开发者接收到事件消息后就可以给用户下发欢迎消息
                responseMessage = handleEventMessage(map);*/
            default:
                customerMsgReq = null;
                break;
        }
        //返回响应消息
        return customerMsgReq;
    }

    /**
     * 接收到文本消息后处理
     * @param map 封装了解析结果的Map
     * @return
     */
    private static CustomerMsgReq handleTextMessage(Map<String, String> map, String appId, String href, String link) {
        CustomerMsgReq customerMsgReq = new CustomerMsgReq();
        Map<String, String> text = new HashMap<>(1);
        text.put("content", "<a href=\""+href+"\" data-miniprogram-appid=\""+appId+"\">"+link+"</a>");
        customerMsgReq.setText(text);
        customerMsgReq.setTouser(map.get("FromUserName"));
        customerMsgReq.setMsgtype(MessageType.TEXT.name().toLowerCase());
        return customerMsgReq;
       /* customerMsgReq.setText("<a href=\""+href+"\">"+text+"</a>");
        //链接内容
        String msgText ="<a href=\""+href+"\">"+text+"</a>";ZZ
        //发送方帐号
        String fromUserName = map.get("FromUserName");
        // 开发者微信号
        String toUserName = map.get("ToUserName");

        */
        /*return String.format( "<xml>" +
                "<ToUserName><![CDATA[%s]]></ToUserName>" +
                "<FromUserName><![CDATA[%s]]></FromUserName>" +
                "<CreateTime>%s</CreateTime>" +
                "<MsgType><![CDATA[text]]></MsgType>" +
                "<Content><![CDATA[%s]]></Content>" +
                "</xml>", fromUserName, toUserName, getMessageCreateTime(), msgText);*/
    }


    /**
     * 生成消息创建时间 （整型）
     * @return 消息创建时间
     */
    private static String getMessageCreateTime() {
        Date dt = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMddhhmm");
        String nowTime = df.format(dt);
        long dd = (long) 0;
        try {
            dd = df.parse(nowTime).getTime();
        } catch (Exception e) {

        }
        return String.valueOf(dd);
    }
}
